package com.jy.adscm.operation.mscyb.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.mscyb.bean.JqbzSettings;
import com.jy.adscm.operation.mscyb.bean.QueryJqbzSettings;
import com.jy.adscm.operation.mscyb.service.JqbzSettingsService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.JyDrawingType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *机器部装配置设置
 * @author zyl
 * @since 2019-05-20
 */
@RestController
@RequestMapping("/adscm/operation/jqbzSettings")
public class JqbzSettingsController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    JqbzSettingsService jqbzSettingsService;

    /**
     * 新增部件配置
     * @param jqbzSettings
     * @param request
     * @return
     */
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody JqbzSettings jqbzSettings, HttpServletRequest request){
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqbzSettings.setCreator(token.getUser().getId().toString());
        jqbzSettings.setCreateTime(new Date());
        jqbzSettings.setIsEnable(1);
        if (AdscmBaseUtils.getInstance().isNotEmpty(jqbzSettings.getUseTime(),null)){
            jqbzSettings.setUseTime(jqbzSettings.getUseTime()/60);
        }
        boolean insert = jqbzSettingsService.insert(jqbzSettings);
        return  new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody JqbzSettings jqbzSettings, HttpServletRequest request) throws AdscmSystemException{
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        jqbzSettings.setCreator(token.getUser().getId().toString());
        jqbzSettings.setCreateTime(new Date());
        boolean result = jqbzSettingsService.updateById(jqbzSettings);
        return  new SimpleCoreResponse("200","修改成功",result);
    }
    @RequestMapping("selectById/{id}")
    public  SimpleCoreResponse  selectByTid(@PathVariable String id) throws  AdscmSystemException{
        JqbzSettings jqbzSettings = jqbzSettingsService.selectJqbzSettingsById(id);
        return  new SimpleCoreResponse("200","查询成功",jqbzSettings);
    }
    @RequestMapping("selectAll")
    public  SimpleCoreResponse  selectAll(@RequestBody QueryJqbzSettings queryJqbzSettings,HttpServletRequest request) throws  AdscmSystemException{
        Page<JqbzSettings>  page=new Page(queryJqbzSettings.getCurrentPage(),queryJqbzSettings.getPageSize());
        page=  jqbzSettingsService.selectAll(page,queryJqbzSettings);

        return  new SimpleCoreResponse("200","查询成功",page);
    }
    @RequestMapping("select")
    public  SimpleCoreResponse  select(HttpServletRequest request) throws  AdscmSystemException{
        List<JqbzSettings> jqbzSettings = jqbzSettingsService.selectAllList();
        return  new SimpleCoreResponse("200","查询成功",jqbzSettings);
    }
    @RequestMapping("selectJyDrawType")
    public  SimpleCoreResponse  selectJyDrawType(HttpServletRequest request) throws  AdscmSystemException{
        List<JyDrawingType> jqbzSettings = jqbzSettingsService.selectJyDrawType();
        return  new SimpleCoreResponse("200","查询成功",jqbzSettings);
    }



}

